#include "HY1C_out.h"
#include <stdio.h>
#include <stdlib.h>
#include "l2_struc.h"
#include "l12_parms.h"
#include "l12_proto.h"

/* --------------------------------------------------------- */
/* init_l2() - initialize a Level-2 record                   */
/* --------------------------------------------------------- */
void init_l2( l2str *l2rec )
{
    char  *p = l2rec->data;

    int32_t  npix  = l2rec->npix;
    //     int32_t  nband = NBANDS;
    int32_t  nband =8;
    int32_t  ip, ib, ipb;

    for (ip=0; ip<npix; ip++) {

        for (ib=0; ib<nband; ib++) {
            ipb = ip*nband+ib;
            l2rec->taua [ipb] = BAD_FLT;
            l2rec->TLg  [ipb] = BAD_FLT;
            l2rec->La   [ipb] = BAD_FLT;
            l2rec->Lw   [ipb] = BAD_FLT;
            l2rec->nLw  [ipb] = BAD_FLT;
            l2rec->nLw_unc[ipb] = 0.0;
            l2rec->brdf [ipb] = BAD_FLT;
            l2rec->Rrs  [ipb] = BAD_FLT;
            l2rec->Rrs_unc[ipb] = 0.0;
            l2rec->a    [ipb] = BAD_FLT;
            l2rec->bb   [ipb] = BAD_FLT;
            l2rec->Rrc  [ipb] = BAD_FLT;
        }

        l2rec->chl[ip] = BAD_FLT;
        l2rec->eps[ip] = BAD_FLT;

        l2rec->aerratio [ip] = BAD_FLT;
        l2rec->aermodmin[ip] = BAD_BYT;
        l2rec->aermodmax[ip] = BAD_BYT;
        l2rec->num_iter [ip] = BAD_INT;
    }
}

void init_l2_single(l2str *l2rec)
{
    static float aerindex = 0;
    l2rec->sensorID = 0;
    l2rec->npix = 0;
    l2rec->nscans = 0;
    l2rec->year = NULL;
    l2rec->day = NULL;
    l2rec->msec = NULL;
    l2rec->lon = NULL;
    l2rec->lat = NULL;
    l2rec->Fo = NULL;
    l2rec->Lr = NULL;
    l2rec->TLg = NULL;
    l2rec->tLf = NULL;
    l2rec->Lt = NULL;
    l2rec->Lt_theroy = NULL;
    l2rec->Lw = NULL;
    l2rec->taua = NULL;
    l2rec->aermodmax = l2rec->aermodmin = 0;
    l2rec->flags = NULL;
    l2rec->solz = NULL;
    l2rec->sola = NULL;
    l2rec->senz = NULL;
    l2rec->sena = NULL;
    l2rec->gain = NULL;
    l2rec->wv = NULL;
    l2rec->aerindex = &aerindex;
    l2rec->invalid = NULL;
    l2rec->La = NULL;
    l2rec->delphi = NULL;
}

void init_l2_single2(l2str *l2rec, int32_t nscan, int32_t npix, int32_t nwave)
{
    static float aerindex = 0;
    l2rec->sensorID = 0;
    l2rec->npix = 0;
    l2rec->nscans = 0;
    l2rec->year = (int32_t*)malloc(sizeof(int));
    l2rec->day = (int32_t*)malloc(sizeof(int));
    l2rec->msec = (int32_t*)malloc(sizeof(int));
    l2rec->lon = (float*)malloc(sizeof(float));
    l2rec->lat = (float*)malloc(sizeof(float));
    l2rec->Fo = (float*)malloc(sizeof(float)*nwave);
    l2rec->Lr = (float*)malloc(sizeof(float)*nwave);
    l2rec->TLg = NULL;
    l2rec->tLf = NULL;
    l2rec->Lt = NULL;
    l2rec->Lt_theroy = NULL;
    l2rec->Lw = NULL;
    l2rec->taua = NULL;
    l2rec->aermodmax = l2rec->aermodmin = 0;
    l2rec->flags = NULL;
    l2rec->solz = NULL;
    l2rec->sola = NULL;
    l2rec->senz = NULL;
    l2rec->sena = NULL;
    l2rec->gain = NULL;
    l2rec->wv = NULL;
    l2rec->aerindex = &aerindex;
    l2rec->invalid = NULL;
    l2rec->La = NULL;
    l2rec->delphi = NULL;
}

void init_l2rec_HY1C(l2rec_HY1C *l2rec, int type)
{
    l2rec->type = type;
    l2rec->sensorID = 0;
    l2rec->npix = 0;
    l2rec->nscans = 0;
    l2rec->modis_matched_flag = NULL;
    l2rec->side_flag = NULL;
    l2rec->year = NULL;
    l2rec->day = NULL;
    l2rec->msec = NULL;
    l2rec->lon = NULL;
    l2rec->lat = NULL;
    l2rec->Fo = NULL;
    l2rec->Lr = NULL;
    l2rec->TLg = NULL;
    l2rec->tLf = NULL;
    l2rec->Lt = NULL;
    l2rec->Lt_theroy = NULL;
    l2rec->Lw = NULL;
    l2rec->taua = NULL;
    l2rec->tg_sol = NULL;
    l2rec->tg_sen = NULL;
    l2rec->aermodmax = l2rec->aermodmin = 0;
    l2rec->flags = NULL;
    l2rec->solz = NULL;
    l2rec->sola = NULL;
    l2rec->senz = NULL;
    l2rec->sena = NULL;
    l2rec->gain = NULL;
    l2rec->wv = NULL;
    l2rec->field_AOT = NULL;
    l2rec->field_Lw = NULL;
    l2rec->field_AOT_lonlat = NULL;
    l2rec->field_Lw_lonlat = NULL;
    l2rec->field_aer_modnum = NULL;
    l2rec->invalid = NULL;
}
